package com.yjs.ppjokedemo.adapter;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.yjs.ppjokedemo.bean.PicProduct;
import com.yjs.ppjokedemo.bean.VideoEntity;
import com.yjs.ppjokedemo.databinding.ItemSofaPicBinding;

import java.io.Serializable;

public class PicListAdapter extends ListAdapter<PicProduct, PicListAdapter.PicViewHolder> {

    PicInterface mPicInterface;

    public PicListAdapter(PicInterface picInterface) {
        super(PicProduct.itemCallback);
        this.mPicInterface = picInterface;
    }

    @Override
    public PicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ItemSofaPicBinding itemSofaPicBinding = ItemSofaPicBinding.inflate(layoutInflater,parent,false);
        itemSofaPicBinding.setPicInterface(mPicInterface);
        return new PicViewHolder(itemSofaPicBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull PicListAdapter.PicViewHolder holder, int position) {
        PicProduct picProduct = getItem(position);
        holder.mItemSofaPicBinding.setPicProduct(picProduct);
        holder.mItemSofaPicBinding.picLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //以key-value(键值对)的形式存在
                Bundle bundle = new Bundle();
                bundle.putString("name",picProduct.getName());
                bundle.putString("ivTou",picProduct.getIvTou());
                bundle.putString("caption",picProduct.getCaption());
                bundle.putString("imagUrl",picProduct.getImagUrl());

//                PicProduct product = new PicProduct(picProduct.getName(),picProduct.getCaption(),picProduct.getIvTou(),picProduct.getImagUrl());
                ARouter.getInstance()
                        .build("/app/SofaPicDetailActivity")
                        .withBundle("dataBean",bundle)
                        .navigation(v.getContext());
            }
        });

    }

    static class PicViewHolder extends RecyclerView.ViewHolder {
        ItemSofaPicBinding mItemSofaPicBinding;

        public PicViewHolder(ItemSofaPicBinding itemSofaPicBinding){
            super(itemSofaPicBinding.getRoot());
            this.mItemSofaPicBinding = itemSofaPicBinding;
        }
    }

    public interface PicInterface{

        void onItemClick(PicProduct picProduct);
    }
}
